<?php
namespace app\command;
use app\admin\model\Orders;
use app\admin\service\ImageService;
use app\admin\service\PhoneService;
use app\admin\service\QrcodeService;
use app\admin\service\MailService;
use app\api\service\GongzonghaoService;
use think\cache\driver\Redis;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\console\input\Option;
use think\console\input\Argument;
use think\Exception;
use app\admin\service\SmsService;
use think\Queue;
use fast\Random;

class ResetOrder extends Command
{
    protected function configure()
    {
        $this->setName('ResetOrder')->setDescription('ceshi');
        $this ->addArgument("domain", Argument::OPTIONAL, "domain");   //参数可有可无   php think Test  openid123456

    }
    protected function execute(Input $input, Output $output)
    {
        //定时查询订单 给他处理为完成或者关闭
        //定时查询云机是否在 还原中是的查询是否结束
        $phoneServiceHandle = new PhoneService();
        $orderModel = new Orders();
        $orderData = db('cloudphone_order')->where('status',$orderModel::STATUS_WAITPAY)->select();
        foreach ($orderData as $oneOrder){
            $c_time = strtotime($oneOrder['create_time']);
            //2分钟未支付则关闭
            if(time()-$c_time > 120){
                db('cloudphone_order')->where('id',$oneOrder['id'])->update(['status'=>$orderModel::STATUS_HASCLOSE]);
            }

        }
        $orderData = db('cloudphone_order')->where('status',$orderModel::STATUS_HASPAY)->select();
        foreach ($orderData as $oneOrder){
            $e_time = strtotime($oneOrder['end_time']);
            if(time()>=$e_time){
                db('cloudphone_order')->where('id',$oneOrder['id'])->update(['status'=>$orderModel::STATUS_HASFINISH]);
                if($oneOrder['type'] == 2){
                    //按天
                    db('cloudphone')->where('id',$oneOrder['phone_id'])->update(['is_use_allday'=>-1]);
                }

                //完成后 重置一下手机
                $reStatus = $phoneServiceHandle->reset($oneOrder['instance_code']);
                if($reStatus == false){
                    $this->vvlog("云机{$oneOrder['phone_id']}执行重置手机失败：".$phoneServiceHandle->errorMsg,'phone_task_error');
                }
            }
        }


        sleep(5);
        $phoneData = db('cloudphone')->where("init_status",2)->select();
        foreach ($phoneData as $phone){
            if(empty($phone['task_id'])){
                db('cloudphone')->where("id",$phone['id'])->update(['init_status'=>1]);
            }else{
                $res = $phoneServiceHandle->taskInfo($phone['instance_code'],$phone['task_id']);
                if($res == false){
                    $this->vvlog("云机{$phone['id']}查询任务执行情况：".$phoneServiceHandle->errorMsg,'phone_task_error');
                }
            }

        }

    }
    public function vvlog($msg,$index){
        var_dump($msg);
        trace_log($msg,$index);
    }
}
